package com.info.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;
/**
 * @author HAHA
 * 
 */
public class ConfigManager {
	private static Logger logger=Logger.getLogger(ConfigManager.class);
	private Properties properties;
	public static int gatePort = 8909;
	public static String gateIp = "127.0.0.1";
	public static String jdrivers = "org.gjt.mm.mysql.Driver";
	public static String jurl = "jdbc:mysql://127.0.0.1:3306/wulianwang?useUnicode=true&characterEncoding=GBK";
	public static String juser = "root";
	public static String jpass = "123";
	public static String jname = "wulianwang";
	public final static BigDecimal bd=new BigDecimal(0);
	public final static BigDecimal jqd=new BigDecimal(0.0005);//精确度
	
	public static int jmax = 10;
	public static int jmin = 1;
	
	public static int readSize = 512000;
	public static int sendSize = 512000;
	public static int maxSize = 3000;
	public static int bufferReadSize = 10240;
	public static int bufferSendSize = 10240;
	
	public static String success="01";//成功
	public static String failes="00";//失败
	public static String putInfo="01"; //下发设置
	public static String readBack="02"; //读取回馈
	public static String putBack="03"; //下发回馈
	public static String readInfo="00"; //下发读取
	public static String[] timezone={"+1","+2","+3","+4","+5","+6","+7","+8","+9","+10","+11","+12",
        "-1","-2","-3","-4","-5","-6","-7","-8","-9","-10","-11","-12"};//时区
	public static String[] linenum={"L1","L2","L3","L4"};//线路编号
	
	public void read(String configFile) {
		this.properties = new Properties();
		String address = "";
		InputStream is = null;
		try {
			URL url = getClass().getProtectionDomain().getCodeSource()
					.getLocation();
			address = url.getFile();
			address = address.replaceAll("%20", " ");
			int index = address.lastIndexOf("/");
			if (index > 3) {
				address = address.substring(0, index + 1);
			}
			address = address + configFile;
			address = StringTools.UTF8Decode(address);
			url = new File(address).toURL();
			this.properties.load(url.openStream());
			if (properties != null) {
				try {
					gatePort = Integer
							.parseInt((String) properties.get("gateport"));
					if (gatePort <= 0)
						gatePort = 8909;
				} catch (Exception e) {
					gatePort = 8909;
				}
				try {
					gateIp = (String) properties.get("gateip");
					if ((gateIp == null) || (gateIp.equals("")))
						gateIp = "127.0.0.1";
				} catch (Exception e) {
					gateIp = "127.0.0.1";
				}
				try {
					jdrivers = (String) properties.get("jdbcdriver");
					if ((jdrivers == null) || (jdrivers.equals("")))
						jdrivers = "org.gjt.mm.mysql.Driver";
				} catch (Exception e) {
					jdrivers = "org.gjt.mm.mysql.Driver";
				}
				try {
					jurl = (String) properties.get("jdbcurl");
					if ((jurl == null) || (jurl.equals(""))) {
						jurl = "jdbc:mysql://127.0.0.1:3306/wulianwang?useUnicode=true&characterEncoding=GBK";
					}
				} catch (Exception e) {
					jurl = "jdbc:mysql://127.0.0.1:3306/wulianwang?useUnicode=true&characterEncoding=GBK";
				}
				try {
					juser = (String) properties.get("jdbcuser");
					if ((juser == null) || (juser.equals(""))) {
						juser = "root";
					}
				} catch (Exception e) {
					juser = "root";
				}
				try {
					jpass = (String) properties.get("jdbcpwd");
					if ((jpass == null) || (jpass.equals(""))) {
						jpass = "123";
					}
				} catch (Exception e) {
					jpass = "123";
				}
				try {
					jmax = Integer.parseInt((String) properties.get("jdbcmax"));
					if (jmax <= 0) {
						jmax = 5;
					}
				} catch (Exception e) {
					jmax = 5;
				}
				try {
					jmin = Integer.parseInt((String) properties.get("jdbcmin"));
					if (jmin <= 0) {
						jmin = 2;
					}
				} catch (Exception e) {
					jmin = 2;
				}
				try {
					jname = (String) properties.get("jndiname");
					if ((jname == null) || (jname.equals(""))) {
						jname = "wulianwang";
					}
				} catch (Exception e) {
					jname = "wulianwang";
				}}
			logger.info("SHW#读取配置文件成功");
		} catch (Exception e) {
			e.printStackTrace();
			if (this.properties != null)
				this.properties.clear();
			this.properties = null;
			logger.info("SHW#读取配置文件失败,地址=" + address);
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException localIOException) {
			}
		}
	}

	public String getValue(String key) {
		if (this.properties != null)
			return this.properties.getProperty(key);
		return null;
	}
}